home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Chat & Communication / Digsby build 37 / digsby_setup.exe / lib / gui / infobox / htmlbitmaps.pyo (.txt) < prev    next >
Python Compiled Bytecode  |  2008-10-13  |  11KB  |  352 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. from __future__ import with_statement
  5. from __future__ import division
  6. import wx
  7. from util import traceguard, urlcacheopen
  8. from util.httplib2 import cached_content
  9. from cStringIO import StringIO
  10. from traceback import print_exc
  11. from gui import skin
  12. from gui.buddylist.renderers import get_buddy_icon
  13. from common import profile
  14. from common import pref
  15. import logging
  16. log = logging.getLogger('gui.htmlbitmaps')
  17. import PIL
  18. from wx import EVT_ERASE_BACKGROUND, EVT_PAINT, EVT_LEFT_UP, EVT_MOTION, RectS, ImageFromString, Panel, BitmapFromImage, AutoBufferedPaintDC, WHITE_BRUSH, TRANSPARENT_PEN, StockCursor, CURSOR_HAND, CURSOR_DEFAULT, Bitmap, BG_STYLE_CUSTOM, ImageFromBitmap, EmptyImage, GetApp, Size
  19. from wx.html import HtmlLinkInfo, HtmlLinkEvent
  20. HTML_ELEM_POS = (-1000, -1000)
  21.  
  22. class BitmapFromData(Panel):
  23.     
  24.     def __init__(self, parent, size = (-1, -1), padding = (0, 0), data = None, href = None):
  25.         self.href = href
  26.         traceguard.__enter__()
  27.         
  28.         try:
  29.             self.data = data.decode('base64')
  30.             self.image = ImageFromString(self.data)
  31.             self.tofinger = True
  32.             Panel.__init__(self, parent, size = size + padding, pos = HTML_ELEM_POS)
  33.             self.SetBackgroundStyle(BG_STYLE_CUSTOM)
  34.             self.bitmap = BitmapFromImage(self.image.Rescale(*size))
  35.             if not self.bitmap.IsOk():
  36.                 self.bitmap = wx.EmptyBitmap(*size)
  37.             
  38.             self.SetBackgroundStyle(wx.BG_STYLE_CUSTOM)
  39.             Bind = self.Bind
  40.             Bind(EVT_PAINT, self.OnPaint)
  41.             Bind(EVT_LEFT_UP, self.OnLeftUp)
  42.             Bind(EVT_MOTION, self.OnMouseMotion)
  43.         finally:
  44.             pass
  45.  
  46.  
  47.     
  48.     def OnPaint(self, event):
  49.         traceguard.__enter__()
  50.         
  51.         try:
  52.             dc = AutoBufferedPaintDC(self)
  53.             rect = RectS(self.Size)
  54.             dc.SetBrush(WHITE_BRUSH)
  55.             dc.SetPen(TRANSPARENT_PEN)
  56.             dc.DrawRectangleRect(rect)
  57.             dc.DrawBitmap(self.bitmap, 0, 0, True)
  58.         finally:
  59.             pass
  60.  
  61.  
  62.     
  63.     def OnMouseMotion(self, event):
  64.         inside = RectS(self.bitmap.Size).Contains(event.Position)
  65.         if inside and self.tofinger:
  66.             self.tofinger = False
  67.             self.SetCursor(StockCursor(CURSOR_HAND))
  68.         elif not inside and not (self.tofinger):
  69.             self.tofinger = True
  70.             self.SetCursor(StockCursor(CURSOR_DEFAULT))
  71.         
  72.  
  73.     
  74.     def OnLeftUp(self, event):
  75.         traceguard.__enter__()
  76.         
  77.         try:
  78.             if self.href is None:
  79.                 return None
  80.             
  81.             if RectS(self.bitmap.Size).Contains(event.Position):
  82.                 linkinfo = HtmlLinkInfo(self.href)
  83.                 evt = HtmlLinkEvent(-1, linkinfo)
  84.                 self.Parent.AddPendingEvent(evt)
  85.                 self.Top.Hide()
  86.         finally:
  87.             pass
  88.  
  89.  
  90.  
  91.  
  92. class BitmapFromSkin(Panel):
  93.     
  94.     def __init__(self, parent, key, size = (-1, -1), padding = (0, 0)):
  95.         traceguard.__enter__()
  96.         
  97.         try:
  98.             Panel.__init__(self, parent, pos = HTML_ELEM_POS)
  99.             incoming = skin.get(key)
  100.             self.bitmap = traceguard if isinstance(incoming, Bitmap) else incoming.GetBitmap(size)
  101.             if not self.bitmap.IsOk():
  102.                 self.bitmap = wx.EmptyBitmap(*size)
  103.             
  104.             self.Size = wx.Size(self.bitmap.Size.width, self.bitmap.Size.height + padding[0] + padding[1])
  105.             self.padding = padding
  106.             self.Bind(EVT_PAINT, self.OnPaint)
  107.             self.SetBackgroundStyle(BG_STYLE_CUSTOM)
  108.         finally:
  109.             pass
  110.  
  111.  
  112.     
  113.     def OnPaint(self, event):
  114.         traceguard.__enter__()
  115.         
  116.         try:
  117.             dc = AutoBufferedPaintDC(self)
  118.             rect = RectS(self.Size)
  119.             dc.Brush = WHITE_BRUSH
  120.             dc.Pen = TRANSPARENT_PEN
  121.             dc.DrawRectangleRect(rect)
  122.             dc.DrawBitmap(self.bitmap, 0, self.padding[0], True)
  123.         finally:
  124.             pass
  125.  
  126.  
  127.  
  128.  
  129. class BuddyIcon(Panel):
  130.     
  131.     def __init__(self, parent, contid, size = None):
  132.         traceguard.__enter__()
  133.         
  134.         try:
  135.             (proto, acct, buddy) = contid.split('/', 2)
  136.             contact = GetApp().StupidContactHack
  137.             icosize = pref('infobox.iconsize', 64)
  138.             self.bitmap = get_buddy_icon(contact, icosize, round_size = 0, grey_offline = False)
  139.             if not self.bitmap.IsOk():
  140.                 self.bitmap = wx.EmptyBitmap(*icosize)
  141.             
  142.             Panel.__init__(self, parent, size = (icosize, icosize), pos = HTML_ELEM_POS)
  143.             self.SetBackgroundStyle(BG_STYLE_CUSTOM)
  144.             self.statico = skin.get('statusicons.' + contact.status_orb).ResizedSmaller(16)
  145.             self.Bind(EVT_PAINT, self.OnPaint)
  146.             self.Bind(EVT_ERASE_BACKGROUND, (lambda e: pass))
  147.             self.SetBackgroundStyle(BG_STYLE_CUSTOM)
  148.         finally:
  149.             pass
  150.  
  151.  
  152.     
  153.     def OnPaint(self, event):
  154.         traceguard.__enter__()
  155.         
  156.         try:
  157.             dc = AutoBufferedPaintDC(self)
  158.             rect = RectS(self.Size)
  159.             statico = self.statico
  160.             dc.SetPen(TRANSPARENT_PEN)
  161.             dc.SetBrush(WHITE_BRUSH)
  162.             dc.DrawRectangleRect(rect)
  163.             dc.DrawBitmap(self.bitmap, 0, 0, True)
  164.             dc.DrawBitmap(statico, (rect.width - 16) + (16 - statico.Size.width) // 2, 0, True)
  165.         finally:
  166.             pass
  167.  
  168.  
  169.  
  170.  
  171. class BitmapFromWeb(Panel):
  172.     
  173.     def __init__(self, parent, url, size = None, href = None, padding = 8, keep_aspect = False, use_image_size = False):
  174.         traceguard.__enter__()
  175.         
  176.         try:
  177.             self.url = url
  178.             self.href = href
  179.             self.padding = padding
  180.             self.tofinger = True
  181.             if url.startswith('skin://'):
  182.                 skinpath = url[len('skin://'):]
  183.                 bitmap = skin.get(skinpath)
  184.                 if use_image_size:
  185.                     size = bitmap.Size
  186.                 
  187.                 if keep_aspect:
  188.                     size = _fix_size(size, bitmap.Size)
  189.                 
  190.                 bitmap = bitmap.Resized(size)
  191.             else:
  192.                 
  193.                 try:
  194.                     image = self._get_pil_url(self.url)
  195.                 except Exception:
  196.                     traceguard
  197.                     traceguard
  198.                     bitmap = None
  199.                     if size is None:
  200.                         size = (45, 0)
  201.                     
  202.                 except:
  203.                     size is None
  204.  
  205.                 if use_image_size:
  206.                     size = image.size
  207.                 
  208.                 if keep_aspect:
  209.                     size = _fix_size(size, image.size)
  210.                 
  211.                 bitmap = image.Resized(size).WXB
  212.             if bitmap is None or not bitmap.IsOk():
  213.                 self.bitmap = wx.EmptyBitmap(*size)
  214.             else:
  215.                 self.bitmap = bitmap
  216.             Panel.__init__(self, parent, size = size + (padding, padding), pos = HTML_ELEM_POS)
  217.             self._init_events()
  218.         finally:
  219.             pass
  220.  
  221.  
  222.     
  223.     def _get_pil_url(self, url):
  224.         
  225.         try:
  226.             f = _[2]
  227.             img = PIL.Image.open(f)
  228.             img.load()
  229.             return img
  230.         finally:
  231.             pass
  232.  
  233.  
  234.     
  235.     def _init_events(self):
  236.         self.SetBackgroundStyle(BG_STYLE_CUSTOM)
  237.         Bind = self.Bind
  238.         Bind(EVT_PAINT, self.OnPaint)
  239.         Bind(EVT_ERASE_BACKGROUND, (lambda e: pass))
  240.         Bind(EVT_LEFT_UP, self.OnLeftUp)
  241.         Bind(EVT_MOTION, self.OnMouseMotion)
  242.  
  243.     
  244.     def OnPaint(self, event):
  245.         dc = AutoBufferedPaintDC(self)
  246.         rect = RectS(self.Size)
  247.         dc.SetBrush(WHITE_BRUSH)
  248.         dc.SetPen(TRANSPARENT_PEN)
  249.         dc.DrawRectangleRect(rect)
  250.         dc.DrawBitmap(self.bitmap, 0, 0, True)
  251.  
  252.     
  253.     def OnMouseMotion(self, event):
  254.         if not self.href:
  255.             return None
  256.         
  257.         inside = RectS(self.bitmap.Size).Contains(event.Position)
  258.         if inside and self.tofinger:
  259.             self.tofinger = False
  260.             self.SetCursor(StockCursor(CURSOR_HAND))
  261.         elif not inside and not (self.tofinger):
  262.             self.tofinger = True
  263.             self.SetCursor(StockCursor(CURSOR_DEFAULT))
  264.         
  265.  
  266.     
  267.     def OnLeftUp(self, event):
  268.         if not self.href:
  269.             return None
  270.         
  271.         if RectS(self.bitmap.Size).Contains(event.Position):
  272.             linkinfo = HtmlLinkInfo(self.href)
  273.             evt = HtmlLinkEvent(-1, linkinfo)
  274.             self.Parent.AddPendingEvent(evt)
  275.             self.Top.Hide()
  276.         
  277.  
  278.  
  279.  
  280. class BitmapFromWebx2(BitmapFromWeb):
  281.     
  282.     def __init__(self, parent, url1, url2, size = None, href = None, padding = 8, keep_aspect = False, size1 = None, size2 = None, pos1 = (0, 0), pos2 = (3, 3)):
  283.         traceguard.__enter__()
  284.         
  285.         try:
  286.             self.url1 = url1
  287.             self.url2 = url2
  288.             self.href = href
  289.             self.padding = padding
  290.             self.tofinger = True
  291.             self.pos1 = pos1
  292.             self.pos2 = pos2
  293.             
  294.             try:
  295.                 image1 = self._get_pil_url(self.url1)
  296.                 image2 = self._get_pil_url(self.url2)
  297.             except Exception:
  298.                 traceguard
  299.                 traceguard
  300.                 bitmap1 = None
  301.                 bitmap2 = None
  302.                 if size is None:
  303.                     size = (45, 0)
  304.                 
  305.             except:
  306.                 size is None
  307.  
  308.             if keep_aspect:
  309.                 size1 = _fix_size(size1, image1.GetSize())
  310.                 size2 = _fix_size(size2, image1.GetSize())
  311.             
  312.             bitmap1 = image1.Resized(size1).WXB
  313.             bitmap2 = image2.Resized(size2).WXB
  314.             if bitmap1 is None or not bitmap1.IsOk():
  315.                 self.bitmap1 = wx.EmptyBitmap(*size1)
  316.             else:
  317.                 self.bitmap1 = bitmap1
  318.             if bitmap2 is None or not bitmap2.IsOk():
  319.                 self.bitmap2 = wx.EmptyBitmap(*size2)
  320.             else:
  321.                 self.bitmap2 = bitmap2
  322.             Panel.__init__(self, parent, size = size + (padding, padding), pos = HTML_ELEM_POS)
  323.             self._init_events()
  324.         finally:
  325.             pass
  326.  
  327.  
  328.     
  329.     def OnPaint(self, event):
  330.         dc = AutoBufferedPaintDC(self)
  331.         rect = RectS(self.Size)
  332.         dc.SetBrush(WHITE_BRUSH)
  333.         dc.SetPen(TRANSPARENT_PEN)
  334.         dc.DrawRectangleRect(rect)
  335.         dc.DrawBitmap(self.bitmap1, self.pos1[0], self.pos1[1], True)
  336.         dc.DrawBitmap(self.bitmap2, self.pos2[0], self.pos2[1], True)
  337.  
  338.  
  339.  
  340. def _fix_size(target, current, scaleto = 1):
  341.     (wt, ht) = target
  342.     (wc, hc) = current
  343.     ratio = wc / hc
  344.     ratio = ratio * scaleto
  345.     if wt:
  346.         ht = ratio ** -1 * wt
  347.     elif ht:
  348.         wt = ratio * ht
  349.     
  350.     return Size(wt, ht)
  351.  
  352.